package org.study.tk.boot.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.study.tk.boot.common.BaseServiceImpl;
import org.study.tk.boot.mapper.StudentMapper;
import org.study.tk.boot.model.Student;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2021-04-20
 */
@Service
public class StudentService2Impl extends BaseServiceImpl<Student> implements StudentService2 {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Student> selectAll() {
        return studentMapper.selectAll();
    }

    @Override
    public List<Student> selectByExample(Example example) {
        return studentMapper.selectByExample(example);
    }

    @Override
    public PageInfo<Student> startPage1(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> selectAll = studentMapper.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (!CollectionUtils.isEmpty(selectAll)) {
            pageInfo = new PageInfo<>(selectAll);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> startPage2(int pageNum, int pageSize) {
        Page<Student> studentPage = PageHelper.startPage(pageNum, pageSize);
        List<Student> selectAll = studentMapper.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (!CollectionUtils.isEmpty(selectAll)) {
            pageInfo = new PageInfo<>(studentPage);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> offsetPage1(int pageNum, int pageSize) {
        PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        List<Student> selectAll = studentMapper.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (!CollectionUtils.isEmpty(selectAll)) {
            pageInfo = new PageInfo<>(selectAll);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> offsetPage2(int pageNum, int pageSize) {
        Page<Student> studentPage = PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        List<Student> selectAll = studentMapper.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (!CollectionUtils.isEmpty(selectAll)) {
            pageInfo = new PageInfo<>(studentPage);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> doSelectPageInfoByStartPage(int pageNum, int pageSize) {
        PageInfo<Student> pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> studentMapper.selectAll());
        return pageInfo;
    }

    @Override
    public PageInfo<Student> doSelectPageInfoByOffsetPage(int pageNum, int pageSize) {
        PageInfo<Student> pageInfo = PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize)
                .doSelectPageInfo(() -> studentMapper.selectAll());
        return pageInfo;
    }

    @Override
    public long count() {
        long count = PageHelper.count(() -> studentMapper.selectAll());
        return count;
    }
}
